package org.feng.utils.condition;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.feng.entity.Datasource;
import org.feng.utils.ColumnFormat;
import org.feng.utils.HttpUtil;
import org.feng.vo.DatasourceVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author by feng
 * @des 条件构造器
 * @date 2021/4/27 14:03
 */
public class Condition {
    /**
     * 分页构造器
     *
     * @param <T>
     * @return
     */
    public static <T> IPage<T> getPage() {
        HttpServletRequest request = HttpUtil.getRequest();
        String current = request.getParameter("current");
        String size = request.getParameter("size");
        return new Page<>(Long.parseLong(current), Long.parseLong(size));
    }

    /**
     * =条件构造器，默认创建时间 倒叙排序
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * =条件构造器，默认创建时间 倒叙排序
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity, String column, String rule) {
        QueryWrapper<T> wrapper = new QueryWrapper<>(entity);
        if (rule.equalsIgnoreCase("desc")) {
            wrapper.orderByDesc(column);
        } else {
            wrapper.orderByAsc(column);
        }
        return wrapper;
    }

    /**
     * like构造器
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryLikeWrapper(T entity) {
        Map<String, Object> map = JSON.parseObject(JSON.toJSONString(entity), Map.class);
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            wrapper.like(ColumnFormat.humpToLine2(entry.getKey()), entry.getValue());
        }
        wrapper.orderByDesc("create_time");
        return wrapper;
    }

    /**
     * like构造器+规则排序
     *
     * @param entity
     * @param rule
     * @param column
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryLikeWrapper(T entity, String rule, String column) {
        Map<String, Object> map = getMap(entity);
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            wrapper.like(ColumnFormat.humpToLine2(entry.getKey()), entry.getValue());
        }
        if (rule.equalsIgnoreCase("desc")) {
            wrapper.orderByDesc(column);
        } else {
            wrapper.orderByAsc(column);
        }
        return wrapper;
    }

    /**
     * 实体转map
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> Map<String, Object> getMap(T entity) {
        return JSON.parseObject(JSON.toJSONString(entity), new TypeReference<Map<String, Object>>() {
        });
    }

}
